package com.example.medical.controller.admin;

import com.example.medical.entity.dto.AdminSaveStoreroomDto;
import com.example.medical.entity.dto.AdminUpdateStoreroomDto;
import com.example.medical.entity.dto.UserGetStoreroomListDto;
import com.example.medical.service.StoreroomService;
import com.example.medical.utils.common.enums.Code;
import com.example.medical.utils.common.result.Result;
import com.example.medical.utils.config.BaseContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin/storeroom")
public class AdminStoreroomController {

    @Autowired
    private StoreroomService storeroomService;

    @PostMapping("/get_storeroom_list")
    public Result AdminGetStoreroomList(@RequestBody UserGetStoreroomListDto dto){
        return storeroomService.UserGetStoreroomList(dto);
    }

    @GetMapping("/get_storeroom_detail")
    public Result AdminGetStoreroomDetail(@RequestParam("id")Long id){
        if (BaseContext.getCurrentId()==null) {
            return Result.errorResult(Code.NEED_LOGIN);
        }
        return Result.okResult(storeroomService.getById(id));
    }

    @GetMapping("/update_get_storeroom")
    public Result AdminUpdateGetStoreroom(@RequestParam("id")Long id){
        if (BaseContext.getCurrentId()==null) {
            return Result.errorResult(Code.NEED_LOGIN);
        }
        return Result.okResult(storeroomService.getById(id));
    }

    @PostMapping("/update_storeroom")
    public Result AdminUpdateStoreroom(@RequestBody AdminUpdateStoreroomDto dto){
        return storeroomService.AdminUpdateStoreroom(dto);
    }

    @PostMapping("/save_storeroom")
    public Result AdminSaveStoreroom(@RequestBody AdminSaveStoreroomDto dto){
        return storeroomService.AdminSaveStoreroom(dto);
    }

    @GetMapping("/remove_storeroom_arr")
    public Result AdminRemoveStoreroomArr(@RequestParam("ids")String[] ids){
        return storeroomService.AdminRemoveStoreroomArr(ids);
    }

    @GetMapping("/remove_storeroom")
    public Result AdminRemoveStoreroom(@RequestParam("id")Long id){
        if (BaseContext.getCurrentId()==null) {
            return Result.errorResult(Code.NEED_LOGIN);
        }
        boolean checkRemove = storeroomService.removeById(id);
        if (!checkRemove) {
            return Result.errorResult(Code.SERVER_ERROR);
        }
        return Result.okResult(Code.SUCCESS);
    }

}
